class Solution
{
public:
    string decodeString(string s)
    {
        string ans;
        stack<int> st_num;
        stack<string> st;
        st.push("");
        for (int i = 0; i < s.size();)
        {
            if (isdigit(s[i]))
            {
                int num = 0;
                while (isdigit(s[i]))
                    num = num * 10 + (s[i] - '0'), ++i;
                st_num.push(num);
            }
            if (s[i] == '[')
            {
                string tmp;
                while (islower(s[++i]))
                    tmp += s[i];
                st.push(move(tmp));
                // 出循环后，i指向第一个不是字母的数
            }
            else if (s[i] == ']')
            {
                int num = st_num.top();
                st_num.pop();
                string s = move(st.top());
                st.pop();
                if (st.size())
                    while (num--)
                        st.top() += s;
                else
                {
                    string tmp;
                    while (num--)
                        tmp += s;
                    st.push(tmp);
                }
                ++i;
            }
            else
            {
                while (islower(s[i]))
                    st.top() += s[i], ++i;
            }
        }
        return st.top();
    }
};